Skip to content

Add FindValueOfExpression function to evaluate symbolic expressions using solution values#20

Merged
kwesiRutledge merged 2 commits intomainfrom
copilot/fix-d059d7bd-ad69-4883-8989-55d25568659d
Oct 7, 2025
Merged

Add FindValueOfExpression function to evaluate symbolic expressions using solution values#20
kwesiRutledge merged 2 commits intomainfrom
copilot/fix-d059d7bd-ad69-4883-8989-55d25568659d

Conversation

Copy link
Contributor

Copilot AI commented Oct 4, 2025

Overview

This PR adds a new FindValueOfExpression function to the solution package that evaluates symbolic expressions using variable values from a solution object. This addresses the need to compute the value of arbitrary expressions after solving an optimization problem.

Motivation

When working with optimization problems, users often need to evaluate expressions (e.g., objective functions, constraint expressions, or derived quantities) using the optimal variable values found by the solver. Previously, there was no convenient way to do this programmatically using the Solution interface.

Changes

New Function

func FindValueOfExpression(s Solution, expr symbolic.Expression) (float64, error)

Parameters:

  • s Solution: A solution object containing variable values
  • expr symbolic.Expression: A symbolic expression to evaluate

Returns:

  • float64: The evaluated value of the expression
  • error: An error if any variable in the expression is missing from the solution

Implementation

The function:

  1. Extracts all variables from the symbolic expression
  2. Retrieves each variable's value from the solution using the existing ExtractValueOfVariable helper
  3. Creates a substitution map to replace variables with constants
  4. Substitutes all variables with their values from the solution
  5. Returns the resulting constant value

Usage Example

// Create variables and a solution
x := symbolic.NewVariable()
y := symbolic.NewVariable()

sol := solution.DummySolution{
    Values: map[uint64]float64{
        x.ID: 2.0,
        y.ID: 3.0,
    },
    Status: solution_status.OPTIMAL,
}

// Evaluate expression: 2*x + 3*y = 2*2 + 3*3 = 13
expr := x.Multiply(symbolic.K(2.0)).Plus(y.Multiply(symbolic.K(3.0)))
value, err := solution.FindValueOfExpression(&sol, expr)
// value = 13.0

Testing

Added 5 comprehensive test cases covering:

  • Linear expressions with multiple variables
  • Constant expressions (no variables)
  • Single-variable expressions
  • Error handling for missing variables
  • Complex expressions with nested operations

All tests pass successfully.

Design Decisions

  • Uses the existing Solution interface methods, maintaining compatibility with any implementation
  • Leverages the ExtractValueOfVariable helper for consistency with existing code
  • Provides clear error messages when variables are missing or unexpected types are encountered
  • Follows the repository's error handling patterns and code style
Original prompt

Can you create a function

solution.FindValueOfExpression(s solution.Solution, expr symbolic.Expression) (float64, error)

that addresses issue #17 using the functions that we have built for the solution.Solution interface?


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: kwesiRutledge <9002730+kwesiRutledge@users.noreply.github.com>
Copilot AI changed the title [WIP] Can you create a function solution.FindValueOfExpression(s solution.Solution, expr symbolic.Expression) (float64, error) that addresses issue #17 using the functions that we have built for the solution.Solution interface? Add FindValueOfExpression function to evaluate symbolic expressions using solution values Oct 4, 2025
Copilot AI requested a review from kwesiRutledge October 4, 2025 19:04
@kwesiRutledge kwesiRutledge linked an issue Oct 5, 2025 that may be closed by this pull request
@kwesiRutledge kwesiRutledge marked this pull request as ready for review October 5, 2025 03:55
@codecov
Copy link

codecov bot commented Oct 5, 2025

Codecov Report

❌ Patch coverage is 77.77778% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.41%. Comparing base (0214315) to head (f8bd25a).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
solution/solution.go 77.77% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #20      +/-   ##
==========================================
+ Coverage   86.76%   87.41%   +0.64%     
==========================================
  Files          27       35       +8     
  Lines        3687     4163     +476     
==========================================
+ Hits         3199     3639     +440     
- Misses        440      470      +30     
- Partials       48       54       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kwesiRutledge kwesiRutledge merged commit 6853593 into main Oct 7, 2025
4 of 5 checks passed
@kwesiRutledge kwesiRutledge deleted the copilot/fix-d059d7bd-ad69-4883-8989-55d25568659d branch October 7, 2025 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding More Features for Extracting Values from Solutions

2 participants